package com.simen.hellobytes.code;

import com.simen.hellobytes.SystemUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

/**
 * @author Created by Simen.
 * @date 创建日期 2018/11/14 11:29
 * @modify 修改者 Simen
 */
public class GrammarUtil {

    public static final int CHAR_TYPE_NONE = 0x0;
    public static final int CHAR_TYPE_OUTOFLINE = 0x5;
    public static final int CHAR_TYPE_STAR = 0x1; // '*'
    public static final int CHAR_TYPE_SLASH = 0x2;// '/'
    public static final int CHAR_TYPE_BACKSLASH = 0x6;// '\'
    public static final int CHAR_TYPE_QUOTE = 0x3;// '''
    public static final int CHAR_TYPE_DOUBLE_QUOTE = 0x4;// '"'

    /**
     * 判断代码是否包含完整的闭包符号{}
     *
     * @param code
     * @return
     */
    public static boolean isClosure(String code) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new StringReader(code));
        StringBuilder newStrBuilder = new StringBuilder();
        String line = null;
        FilterCommentLine filterCommentLine = new FilterCommentLine();
        while ((line = bufferedReader.readLine()) != null) {
            String noCommentLine = filterCommentLine.swallowLine(line);
            if (noCommentLine != null) {
                newStrBuilder.append(noCommentLine);
            }
            newStrBuilder.append(SystemUtil.getLineSeparator());
        }

        filterCommentLine.swallowEnd();

        String trimCode = newStrBuilder.toString();
        int charL = trimCode.length();

        int numPreBracket = 0;
        int numSuffixBracket = 0;
        for (int i = 0; i < charL; i++) {
            char c = trimCode.charAt(i);

            if (c == '{') {
                numPreBracket++;
            } else if (c == '}') {
                numSuffixBracket++;
            }
        }

        return numPreBracket == numSuffixBracket;
    }

    public static boolean beforeQuoteContainsSingleBackSlash(String noCommentLine, int i) {
        return !beforeQuoteContainsDoubleBackSlash(noCommentLine,i);
    }

    /**
     * 确保'"'前面包含偶数个'\'
     */
    public static boolean beforeQuoteContainsDoubleBackSlash(String noCommentLine, int index) {
        int backSlashNum = 0;
        for (int i = index; i >= 0; i--) {
            if (charType(noCommentLine, i) == GrammarUtil.CHAR_TYPE_BACKSLASH) {
                backSlashNum++;
            } else {
                break;
            }
        }

        return backSlashNum % 2 == 0;
    }

    public static int charType(String str, int current) {
        if (current < 0) return CHAR_TYPE_OUTOFLINE;

        if (current < str.length()) {
            char c = str.charAt(current);

            if (c == '/') {
                return CHAR_TYPE_SLASH;
            } else if (c == '*') {
                return CHAR_TYPE_STAR;
            } else if (c == '\'') {
                return CHAR_TYPE_QUOTE;
            } else if (c == '\"') {
                return CHAR_TYPE_DOUBLE_QUOTE;
            } else if (c == '\\') {
                return CHAR_TYPE_BACKSLASH;
            } else {
                return CHAR_TYPE_NONE;
            }
        }

        return CHAR_TYPE_OUTOFLINE;
    }
}
